//
//  BHMainTabbarController.m
//  PropertyMananger
//
//  Created by Mansion on 16/12/26.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "BHMainTabbarController.h"
#import "BHNavigationController.h"

#import "HomeViewController.h"
#import "WorkViewController.h"
#import "QueryViewController.h"
#import "MineViewController.h"

@interface BHMainTabbarController ()

@property (nonatomic, strong) HomeViewController *homeVc;
@property (nonatomic, strong) WorkViewController *workVc;
@property (nonatomic, strong) QueryViewController *queryVc;
@property (nonatomic, strong) MineViewController *mineVc;

@property (nonatomic,copy) NSString * complainttotalCount;//总投诉单数量
@property (nonatomic,copy) NSString * repairtotalCount;//总报修单单数量

@property (nonatomic,copy) NSString * myComplaintcount;//我的投诉单数量
@property (nonatomic,copy) NSString * myRepaircount;//我的报修单单数量

@end

@implementation BHMainTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
   
  //  [self postWorkNumData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarClick:) name:getTabbarNewsCountNoticeCenter object:nil];
    
    // 添加子控制器
    [self addChildVc:self.homeVc title:@"首页" image:@"tab_home_unselected" selectedImage:@"tab_home_onselected"];
    
    [self addChildVc:self.workVc title:@"工单" image:@"tab_work_order_unselected" selectedImage:@"tab_work_order_onselected"];
    
    [self addChildVc:self.queryVc title:@"查询" image:@"tab_query_unselected" selectedImage:@"tab_query_onselected"];
    
    [self addChildVc:self.mineVc title:@"我的" image:@"tab_my_unselected" selectedImage:@"tab_my_onselected"];


}
- (void)tabBarClick:(NSNotification *)noti{

    DLog(@" 通知获取的 === %@",noti.userInfo[@"key"]);
    //key:总的投诉 key1总的报修  key2:我的投诉 key3我的报修 
     self.complainttotalCount = [NSString stringWithFormat:@"%@",noti.userInfo[@"key"]];
    self.repairtotalCount = [NSString stringWithFormat:@"%@",noti.userInfo[@"key1"]];
    
    DLog(@"总的保修单=== %@ 总的投诉单 === %@",self.repairtotalCount,self.complainttotalCount);
    
    self.myComplaintcount = [NSString stringWithFormat:@"%@",noti.userInfo[@"key2"]];
    self.myRepaircount = [NSString stringWithFormat:@"%@",noti.userInfo[@"key3"]];
    
    if ([self.repairtotalCount isEqualToString:@"0"] && [self.complainttotalCount isEqualToString:@"0"]) {
        //隐藏
        [self.workVc.tabBarController.tabBar hideBadgeOnItemIndex:1];
    }else{
            //显示
            [self.workVc.tabBarController.tabBar showBadgeOnItemIndex:1];
        }
    
    if ([self.myRepaircount isEqualToString:@"0"] && [self.myComplaintcount isEqualToString:@"0"]) {
        
        [self.mineVc.tabBarController.tabBar hideBadgeOnItemIndex:3];
    }else{
    
     [self.mineVc.tabBarController.tabBar showBadgeOnItemIndex:3];
    }

}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
#pragma mark - private methods
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    
    /** 设置tabbar title  和 navigationbar title */
    childVc.title = title;
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :Theme_Color} forState:UIControlStateSelected];
    /** 设置图片 */
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    /** 添加子控制器 */
    BHNavigationController *navigationVc = [[BHNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:navigationVc];
    
}
#pragma mark - getters and setters
- (HomeViewController *)homeVc
{
    if (_homeVc == nil) {
        _homeVc = [[HomeViewController alloc] init];
    }
    return _homeVc;
}
- (WorkViewController *)workVc
{
    if (_workVc == nil) {
        _workVc = [[WorkViewController alloc] init];
      
  
    }
    return _workVc;
}
- (QueryViewController *)queryVc
{
    if (_queryVc == nil) {
        _queryVc = [[QueryViewController alloc] init];
    }
    return _queryVc;
}
- (MineViewController *)mineVc
{
    if (_mineVc == nil) {
        
        _mineVc = [[MineViewController alloc]init];
    }
    return _mineVc;
}
@end
